package com.haipumi.biz.impl;

import java.lang.reflect.Type;
import java.util.Hashtable;
import java.util.Map;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import com.google.gson.reflect.TypeToken;
import com.haipumi.beans.StatisticalModel;
import com.haipumi.biz.RedisStatisticalBiz;
import com.haipumi.redis.GetShardedJedis;
import com.haipumi.utils.CommonData;
import com.haipumi.utils.GsonUtil;

@Service("redisStatisticalBiz")
public class RedisStatisticalBizImpl implements RedisStatisticalBiz{

	@Override
	public StatisticalModel findIndexViewsTotal(String start, String deadline) {
		ShardedJedis jedis=GetShardedJedis.getShardedJedis();//从池中获取redis
		
		String jsonString=(String) jedis.get(CommonData.indexViewsRedisKey);
		String[] abscissaArr =genTimeArray(start,deadline);//获取键的范围
		Integer[] abscissaValueArr=new Integer[abscissaArr.length];
		Map map;
		if(jsonString!=null && jsonString!=""){//如果已经有数据
			Type objectType = new TypeToken< Hashtable<String,Integer> >(){}.getType();
			map=(Hashtable<String,Integer> ) GsonUtil.JsonToObject(jsonString, objectType);//将他转为一个map
		}else{
			map=new Hashtable<String,Integer>();
		}
		//获取横坐标对应的值
		for(int i=0;i<abscissaArr.length;i++){
			if(map.containsKey(abscissaArr[i])){
				abscissaValueArr[i]=(Integer) map.get(abscissaArr[i]);
			}else{
				abscissaValueArr[i]=0;
			}
		}
		StatisticalModel model=new StatisticalModel();
		model.setAbscissa(abscissaArr);
		model.setAbscissaValue(abscissaValueArr);
		GetShardedJedis.ReturnShardedJedis(jedis);
		return model;
	}

	//根据起始时间和结束时间,计算中间的日期 yyyy-MM-dd 
	public String[] genTimeArray(String start, String deadline){
		String[] arr = null;
		String[] startData=start.split("-");
		String[] deadlineData=deadline.split("-");
		
		//TODO 对月份,年份的判断 需要优化
		int monthTemp=Integer.parseInt( deadlineData[1] )-Integer.parseInt( startData[1] ) ;//相差的月份
//		int yearTemp=Integer.parseInt( deadlineData[0] )-Integer.parseInt( startData[0] ) ;//相差的年份
		
		Integer startDay=Integer.parseInt( startData[2] );
		Integer deadlineDay=Integer.parseInt( deadlineData[2] );
		int temp=deadlineDay-startDay;
		arr=new String[temp+1];//同月的情况下
		while(temp>=0){
			Integer day=deadlineDay-temp;
			arr[arr.length-temp-1]=deadlineData[0]+"-"+deadlineData[1]+"-"+day;
			temp--;
		}
		return arr;
	}

	@Override
	public StatisticalModel findIPViewsTotal() {
		ShardedJedis jedis=GetShardedJedis.getShardedJedis();//从池中获取redis
		
		String jsonString=(String) jedis.get(CommonData.IPViewsRedisKey);
		Hashtable<String,Integer> map;
		if(jsonString!=null && jsonString!=""){//如果已经有数据
			Type objectType = new TypeToken< Hashtable<String,Integer> >(){}.getType();
			map=(Hashtable<String,Integer> ) GsonUtil.JsonToObject(jsonString, objectType);//将他转为一个map
		}else{
			map=new Hashtable<String,Integer>();
		}
		String[] abscissaArr =new String[map.size()];//获取键的范围
		Integer[] abscissaValueArr=new Integer[map.size()];
		//遍历map
		int index=0;
		for(Map.Entry<String, Integer> entry:map.entrySet()){
			if(index<map.size()){
				abscissaArr[index]=entry.getKey();
				abscissaValueArr[index]=entry.getValue();
			}
			index++;
		}
		
		StatisticalModel model=new StatisticalModel();
		model.setAbscissa(abscissaArr);
		model.setAbscissaValue(abscissaValueArr);
		
		GetShardedJedis.ReturnShardedJedis(jedis);
		return model;
	}
}
